﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClass;
using System.Data;

namespace QuerySelectionLabelingTool
{
    public partial class Default : System.Web.UI.Page
    {
        CoverageOperation coverageOper = new CoverageOperation();
        protected void Page_Load(object sender, EventArgs e)
        {
            ImportTaskFromOutside task = new ImportTaskFromOutside();
            Session["userID"] = task.LoadUsername(Session["username"].ToString());
        }

        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            foreach (TableRow r in this.GridView2.Rows)
            {
                if (r.Cells[1].Text.ToString() == "&nbsp;")
                {
                    r.Cells[1].Text = "0";
                    r.Cells[3].Controls.Clear();
                    r.Cells[3].Text = "Complete";
                }
            }
            
            //set status column
            DataView dataView = (DataView)(SqlDataSourceMyTask.Select(DataSourceSelectArguments.Empty));
            DataTable table = dataView.Table;
            for (int i = 0; i < this.GridView2.Rows.Count; ++i)
            {

                int tableIndex = this.GridView2.PageIndex * this.GridView2.PageSize + i;
                TableRow r = this.GridView2.Rows[i];
                int taskID = Convert.ToInt32(table.Rows[tableIndex]["taskID"]);
                int inComplete = Convert.ToInt32(r.Cells[1].Text);
                int total = Convert.ToInt32(r.Cells[2].Text);
                if (inComplete == 0)
                {
                    r.Cells[3].Text = "Complete";
                }
                else if (inComplete < total)
                {
                    r.Cells[3].Text = "Starting";
                }
                else if (coverageOper.IsPingBingComplete(taskID) && !coverageOper.IsTaskInQueue(taskID))
                {
                    r.Cells[3].Text = "Ready";
                }
                else
                {
                    r.Cells[3].Text = "Pending";
                }
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            foreach (TableRow r in this.GridView1.Rows)
            {
                //set "Incomplete" is "0"
                if (r.Cells[2].Text.ToString() == "&nbsp;")
                {
                    r.Cells[2].Text = "0";
                }
                //set "Labeling Result" is unavilable if Incomplete == Total
                if (r.Cells[2].Text == r.Cells[3].Text)
                {
                    r.Cells[4].Controls.Clear();
                    r.Cells[4].Text = "No Labeling Result";
                }
                //judge ping bing is "ping bing" or "cancel" state
                LinkButton linkButton = (LinkButton)(r.Cells[7].Controls[1]);
                int taskID = Convert.ToInt32(linkButton.CommandArgument.ToString());
                if (coverageOper.IsTaskInQueue(taskID))
                {
                    linkButton.Text = "Cancel";
                    linkButton.OnClientClick = "javascript:return confirm('Are you sure you want to cancel ping against Bing? No more queries will be sent to Bing after this operation. But the existing ping result will NOT be removed.')";
                }
                else
                {
                    linkButton.Text = "Ping Bing";
                    linkButton.OnClientClick = "javascript:return confirm('Are you sure you want to ping all queries against Bing in this task? This may take several minutes/hours to finish.')";
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            
            string s = ((LinkButton)sender).Text;
            int taskID = Convert.ToInt32(((LinkButton)sender).CommandArgument.ToString());
            if (s == "Ping Bing")
            {
                ((LinkButton)sender).OnClientClick = "javascript:return confirm('Are you sure you want to ping all queries against Bing in this task? This may take several minutes/hours to finish.')";
                ((LinkButton)sender).Text = "Cancel";
                coverageOper.TriggerEvent(taskID);
            }
            else
            {
                ((LinkButton)sender).OnClientClick = "javascript:return confirm('Are you sure you want to cancel ping against Bing? No more queries will be sent to Bing after this operation. But the existing ping result will NOT be removed.')";
                ((LinkButton)sender).Text = "Ping Bing";
                coverageOper.DeleteTaskIDInQueue(taskID);
            }
            //when state changed, change the column "status" of gridview.
            this.GridView2.DataBind();
            this.GridView1.DataBind();
        }
        protected void GridView1_Load(object sender, EventArgs e)
        {
            foreach (TableRow r in this.GridView1.Rows)
            {
                if (r.Cells[2].Text == r.Cells[3].Text)
                {
                    r.Cells[4].Controls.Clear();
                    r.Cells[4].Text = "No Labeling Result";
                }
            }

        }
    }
}